#pragma once
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>

#include <cstdlib>
#include <cstring>
#include <functional>

#include <string>




namespace Net_TcpSocket
{
    const int default_sockfd = -1;
    const int default_backlog = 5;
    class Socket{
    
    protected:
    enum {
        SocketError = 1,
        BindError,
        ListenError
    };
    std::function<struct sockaddr*(void*)> _conv= [](void* addr){
        return (struct sockaddr*)addr;
    };
    public:
    virtual ~Socket()
    {

    }  
    virtual void CreatSocketOrDie() = 0;
    virtual void BindsocketOrDie(uint16_t port) = 0; 
    virtual void LisenSocketOrDie(int backlog) = 0;
    virtual Socket* AcceptConnection(std::string* peerIp,uint16_t * peerPort) =0;
    virtual bool ConnectServer(std::string &serverip,uint16_t ServerPort) = 0;
    virtual int GetSockfd() = 0;
    virtual void SetSockfd(int sockfd) = 0;
    virtual void CloseSocket() = 0;
    virtual  bool Recv(std::string* buffer,int expeact_Size) = 0;
    virtual bool Send(const std::string& buffer) = 0;
    virtual int AcceptRawConnection(std::string* peerIp,uint16_t * peerPort) = 0;
    public:
    void BuildListenSocket(uint16_t port,int backlog)
    {
        CreatSocketOrDie();
        BindsocketOrDie(port);
        LisenSocketOrDie(backlog);
    }
    void BuildConnectSocket(std::string server_ip,uint16_t server_port)
    {
        CreatSocketOrDie();
        ConnectServer(server_ip,server_port);
    }
    void BuildNormalSocket(int sockfd)
    {
        SetSockfd(sockfd);
    }
};


class TcpSocket :public Socket
{
    protected:
    int _sockfd;
    public:
    ~TcpSocket()
    {
        CloseSocket();
    }

    TcpSocket(int sockfd =default_sockfd):_sockfd(sockfd)
    {
        
    }

    
    virtual void CreatSocketOrDie() override
    {
        _sockfd = ::socket(AF_INET,SOCK_STREAM,0);
        if(_sockfd<0) exit(-1);
    }
    virtual void BindsocketOrDie(uint16_t port)override
    {
        struct sockaddr_in local;
        memset(&local,0,sizeof(local));
        local.sin_family = AF_INET;
        local.sin_addr.s_addr = INADDR_ANY;
        local.sin_port = htons(port);
        int n = ::bind(_sockfd,_conv(&local),sizeof(local));
    }
    virtual void LisenSocketOrDie(int backlog) override
    {
        int n = ::listen(_sockfd,backlog);
        if(n<0) exit(ListenError);
    }
    virtual Socket* AcceptConnection(std::string* peerIp,uint16_t * peerPort)override
    {
        struct sockaddr_in peer; 
        socklen_t len = sizeof(peer);

        int newsockfd = ::accept(_sockfd,_conv(&peer),&len);
        if(newsockfd<0) return nullptr;
        *peerPort = ntohs(peer.sin_port);
        *peerIp = inet_ntoa(peer.sin_addr);

        Socket* s = new TcpSocket(newsockfd);
        return s;
    }
    //获取生的套接字文件描述符
    virtual int AcceptRawConnection(std::string* peerIp,uint16_t * peerPort)override
    {
        struct sockaddr_in peer; 
        socklen_t len = sizeof(peer);

        int newsockfd = ::accept(_sockfd,_conv(&peer),&len);
        if(newsockfd<0) return newsockfd;
        *peerPort = ntohs(peer.sin_port);
        *peerIp = inet_ntoa(peer.sin_addr);

        return newsockfd;
    }
    virtual bool ConnectServer(std::string &serverip,uint16_t ServerPort)override
    {
        struct sockaddr_in server;
        memset(&server,0,sizeof(server));
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = inet_addr(serverip.c_str());
        server.sin_port = htons(ServerPort);
        int n = connect(_sockfd,_conv(&server),sizeof(server));
        if(n==0)return 1;
        else return 0;
    }
    virtual int GetSockfd(){
        return _sockfd;
    }
    virtual void SetSockfd(int sockfd){
        _sockfd = sockfd;
    } 
    
    public:
    void BuildListenSocket(uint16_t port,int backlog)
    {
        CreatSocketOrDie();
        BindsocketOrDie(port);
        LisenSocketOrDie(backlog);
    }
    void BuildConnectSocket(std::string server_ip,uint16_t server_port)
    {
        CreatSocketOrDie();
        ConnectServer(server_ip,server_port);
    }
    void BuildNormalSocket(int sockfd)
    {
        SetSockfd(sockfd);
    }
    void CloseSocket()
    {
        ::close(_sockfd);
        _sockfd = -1;
    }
    virtual bool Recv(std::string* buffer,int expeact_Size)override
    {
        char inbuffer[expeact_Size];
        ssize_t n = recv(_sockfd,inbuffer,expeact_Size-1,0);
        if(n>0)
        {
            inbuffer[n] = 0;
            * buffer += inbuffer;
            return 1;
        }
        return 0;
    }

    virtual bool Send(const std:: string& msg) override
    {
        send(_sockfd,msg.c_str(),msg.size(),0);
        return 1;
    }
};

}
